<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class UserRegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'username' => 'required|string|min:2|max:20', //unique参数 第一个哪个表 第二个 哪个字段名
            'email'    => 'required|email|unique:users|max:50|string',
            'password' => 'required|string|min:6|confirmed',
            'mobile'   => 'regex:/^1[345789][0-9]{9}$/|unique:users',
            'captcha'  => 'required|size:4|captcha',
        ];
    }

    public function messages()
    {
        return [
            'username.required'  => '用户名不能为空',
            'username.string'    => '用户名必须为英文字母或者数字',
            'username.min'       => '用户名至少2位',
            'username.max'       => '用户名做多20位',
            'username.unique'    => '用户名已被注册',
            'email.required'     => '邮箱不得为空',
            'email.email'        => '请输入正确的邮箱地址',
            'email.unique'       => '此邮箱已被注册',
            'email.max'          => '邮箱最多50位',
            'password.required'  => '请输入密码',
            'password.min'       => '密码至少6位',
            'password.confirmed' => '两次密码输入不一致',
            'mobile.regex'       => '请输入正确的手机号',
        ];
    }
    /*public function failedValidation(Validator $validator)
    {
        throw (new HttpResponseException(response()->json([
            'message' => $validator->errors()->first(),
        ], 422)));
    }*/
}
